package edu.hunau.cxb19.oop.class11;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Locale;

import org.junit.Test;

/**
 *
 * @author hb
 *
 *         2020-03-10 15:34:23
 */
public class StringTest {

	@Test
	public void test01() {
		String str = "abc123";
		String newStr = str.toUpperCase();// 通过方法产生一个新的字符串
		System.out.println(str);// abc123 ,String一旦初始化之后值不可变
		System.out.println(newStr);// ABC123
		String upperCase = str.toUpperCase(Locale.CHINA);
		System.out.println(upperCase);// ABC123

		System.out.println("abc".equals("Abc"));// false
		System.out.println("abc".equalsIgnoreCase("Abc"));// true

		String filename = "a.txt";
		if (filename.endsWith(".txt")) {
			System.out.println("该文档为一个文本文档");
		}
	}

	@Test
	public void test02() {
		String str = "232sq3taefa";
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			System.out.println(c + "   ");
		}

		String datas = "1-2-3-34-45-4-5-567";
		String[] results = datas.split("-");
		System.out.println(Arrays.toString(results));// [1, 2, 3, 34, 45, 4, 5, 567]

	}

	@Test
	public void test03() throws Exception {
		String str = "abc好人";
		// 当前java文件的默认编码为utf-8
		byte[] bytes = str.getBytes();
		// [97, 98, 99, -27, -91, -67, -28, -70, -70]
		System.out.println(Arrays.toString(bytes));
		byte[] gbkBytes = str.getBytes("GBK");
		// [97, 98, 99, -70, -61, -56, -53]
		System.out.println(Arrays.toString(gbkBytes));
	}

	@Test
	public void test04() throws Exception {
		byte[] bytes = { 97, 98, 99, -27, -91, -67, -28, -70, -70 };
		String datas = new String(bytes);
		System.out.println(datas);
		String str = new String(bytes, "GBK");
		System.out.println(str);//出现乱码，bytes默认采用utf-8,解码使用的时GBk
	}
	
	@Test
	public void test05() {
		String str="abc123abc123";
		System.out.println(str.indexOf("c12"));//2
		System.out.println(str.indexOf("c12",3));//8
		System.out.println(str.indexOf("c2"));//-1
		
		//https://www.hunau.com.edu/cxb18/search.do?sid=1001
	}
	
	@Test
	public void test06() {
		String str="abc123abc123";
		System.out.println(str.replace("123", "***"));//abc***abc***
		System.out.println(str.replaceAll("123", "***"));//abc***abc***
		System.out.println(str.replaceFirst("123", "***"));//abc***abc123
		
		String data="       1 2       3         ";
		System.out.println(data.length());//27
		data=data.trim();
		System.out.println(data.length());//11
		System.out.println(data);//1 2       3
		
	}
}
